package vn.ttpcode.com.sample.listview;

import java.util.ArrayList;

import vn.ttpcode.com.sample.R;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;

public class ListViewAdapter extends BaseAdapter implements OnScrollListener{
	
	private class ViewHolder {
		public TextView name;
		public ImageView imgAvata;
		public int position;
	}
	
	private static final String TAG = "ttpcode";
	private boolean isFlinging = false;
	private boolean isTouching = false;
	
	private ArrayList<ListViewObj> listObj;
	private Context context;
	private LayoutInflater inflater;
	
	public ListViewAdapter(Context context, ArrayList<ListViewObj> listObj){
		this.context = context;
		this.listObj = listObj;
		
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listObj.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listObj.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		View view = convertView;
		final ViewHolder holder;
		if (null == view) {
			holder = new ViewHolder();
			view = inflater.inflate(R.layout.listview_item, null);
			
			holder.name = (TextView) view.findViewById(R.id.txtName);
			holder.imgAvata = (ImageView) view.findViewById(R.id.imgAvata);
			
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		
		holder.position = position;
		
		if(isFlinging){
			Log.i(TAG, "Now flinging ...");
			holder.name.setText("FLINGING ...");
		}else{
			Log.e(TAG, "LOAD DATA: ");
			holder.name.setText(listObj.get(position).getName());
		}
		
		return view;
	}
	
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		Log.e(TAG, "onScrollStateChanged: " + scrollState);
		
		if(OnScrollListener.SCROLL_STATE_FLING == scrollState){
			isFlinging = true;
			isTouching = true;
		}else{
			isFlinging = false;
			
			if(isTouching){
				isTouching = false;
				loadView(view, scrollState);
			}
		}
	}
	
	private void loadView(AbsListView view, int scrollState){
		int count = view.getChildCount();
        for (int i = 0; i < count; i++) {
            View convertView = view.getChildAt(i);
            ViewHolder holder = (ViewHolder) convertView.getTag();
            
            holder.name.setText(listObj.get(holder.position).getName());
            
            Log.i(TAG, "LOAD DATA: " + i + " => " + holder.position);
        }
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		super.notifyDataSetChanged();
		Log.e(TAG, "notifyDataSetChanged");
	}
}
